<template>
  <el-container class="admin-container">
    <el-aside width="200px">
      <el-menu
        :default-active="$route.path"
        router
        class="admin-menu"
        background-color="#304156"
        text-color="#fff"
      >
        <el-menu-item index="/admin">
          <el-icon><Odometer /></el-icon>
          <span>首页</span>
        </el-menu-item>
        <el-menu-item index="/admin/user">
          <el-icon><User /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/study-room">
          <el-icon><School /></el-icon>
          <span>自习室管理</span>
        </el-menu-item>
        <!-- 添加预约管理菜单项 -->
        <el-menu-item index="/admin/reservation">
          <el-icon><Calendar /></el-icon>
          <span>预约管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/feedback">
          <el-icon><ChatDotRound /></el-icon>
          <span>留言管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/announcement">
          <el-icon><Bell /></el-icon>
          <span>公告管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/snack">
          <el-icon><Food /></el-icon>
          <span>零食管理</span>
        </el-menu-item>
        <el-menu-item index="/admin/order">
          <el-icon><ShoppingCart /></el-icon>
          <span>订单管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header>
        <div class="header-left">
          <h2>自习室管理系统</h2>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              {{ userInfo?.username || '未登录' }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import {
  Odometer,
  User,
  School,
  Calendar,  // 添加 Calendar 图标
  ChatDotRound,
  Bell,
  Food,
  ShoppingCart,
  ArrowDown
} from '@element-plus/icons-vue'

export default {
  name: 'Admin',
  components: {
    Odometer,
    User,
    School,
    Calendar,  // 注册 Calendar 组件
    ChatDotRound,
    Bell,
    Food,
    ShoppingCart,
    ArrowDown
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    
    const userInfo = computed(() => store.state.user)
    
    onMounted(() => {
      if (!localStorage.getItem('token') || !store.state.user) {
        router.push('/login')
      }
    })
    
    const handleCommand = (command) => {
      if (command === 'logout') {
        ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          localStorage.removeItem('token')
          store.commit('setUser', null)
          router.push('/login')
        })
      }
    }
    
    return {
      userInfo,
      handleCommand
    }
  }
}
</script>

<style scoped>
.admin-container {
  height: 100vh;
}

.admin-menu {
  height: 100%;
  border-right: none;
}

.el-header {
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.header-right {
  cursor: pointer;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  color: #606266;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style> 